#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__author__ = "Administrator"
__name__="mysql01"
__date__="2016/6/23"
__time__="10:40"
"""
import MySQLdb

"""
# Python 操作mysql之查看数据
# 打开数据库连接
conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='123456', db='object')
# 具体操作
cur = conn.cursor()
reCount = cur.execute('select * from admin')    # 操作影响的条数
data = cur.fetchall()   # 语句获取的内容

# 关闭连接
cur.close()
conn.close()

# 查看返回值
print reCount
print data
"""

'''
# Python操作mysql 之insert
# 打开数据库连接
conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='123456', db='object')
# 具体操作
cur = conn.cursor()

# 把sql语句单独写,用占位符代替参数
sql = "insert into admin(id,name) values(%s,%s)"

# 参数
params = ('3', 'jak')

reCount = cur.execute(sql, params)  # 传入参数，执行语句
conn.commit() # 提交更新到数据库，insert update delete 之后都需要commit

# 关闭连接
cur.close()
conn.close()

print reCount  # 打印影响的条数
'''

"""
# Python操作mysql之delete
# 打开数据库连接
conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='123456', db='object')
# 具体操作
cur = conn.cursor()

# 把sql语句单独写,用占位符代替参数
sql = "delete from admin where id = %s"

# 参数
params = ('1', )

reCount = cur.execute(sql, params)  # 传入参数，执行语句
conn.commit() # 提交更新到数据库，insert update delete 之后都需要commit

# 关闭连接
cur.close()
conn.close()

print reCount  # 打印影响的条数
"""

"""
# Python操作mysql之update
# 打开数据库连接
conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='123456', db='object')
# 具体操作
cur = conn.cursor()

# 把sql语句单独写,用占位符代替参数
sql = "update admin set name = %s where id = %s"

# 参数
params = ('lili','2' )

reCount = cur.execute(sql, params)  # 传入参数，执行语句
conn.commit() # 提交更新到数据库，insert update delete 之后都需要commit

# 关闭连接
cur.close()
conn.close()

print reCount  # 打印影响的条数
"""

"""
# Python操作mysql之批量插入
# 打开数据库连接
conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='123456', db='object')
# 具体操作
cur = conn.cursor()

# 把sql语句单独写,用占位符代替参数
sql = "insert into admin(id,name) values(%s,%s)"

# 参数--元祖--列表
li = [
    ('5', 'tom'),
    ('4', 'jack'),

]

reCount = cur.executemany(sql, li)
conn.commit()
cur.close()
conn.close()

print reCount  # 打印影响的条数
"""

"""
# Python操作mysql之fetchone
# 打开数据库连接
conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='123456', db='object')
cur = conn.cursor()

# 把sql语句单独写,用占位符代替参数
sql = "select * from admin"

# 执行sql
reCount = cur.execute(sql)

# 获取数据
data = cur.fetchone()
print data
cur.scroll(0,mode='absolute')
data = cur.fetchone()
print data

# 指针的两种模式
# cur.scroll(0,mode='absolute')    绝对定位
# cur.scroll(-1,model='relative')  相对定位，相对于当前位置

# fetchmany 跳多条

conn.commit()
cur.close()
conn.close()

"""

"""
# Python操作mysql之自增ID----继续了解
# 打开数据库连接,打开操作是很慢的，尽量一次打开，多次操作。
conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='123456', db='object')
cur = conn.cursor()

# 把sql语句单独写,用占位符代替参数
sql = "insert into admin(name) values(%s)"

params = ('tin',)
# 执行sql
reCount = cur.execute(sql,params)
conn.commit()
new_id = cur.lastrowid
cur.close()
conn.close()

print new_id
"""